package loadutil

import (
	"encoding/json"
	"gitee.com/zhongguo168a/gocodes/datax"
	"gitee.com/zhongguo168a/gocodes/datax/cachex/values"
	"gitee.com/zhongguo168a/gocodes/datax/schemax/configx"
	"gitee.com/zhongguo168a/gocodes/myx/errorx"
	"io/ioutil"
)

func J加载配置文件(path string, set *configx.ConfigRepo) (err error) {
	b, err := ioutil.ReadFile(path)
	if err != nil {
		err = errorx.Wrap(err, "ioutil.ReadFile")
		return
	}
	m := map[string]interface{}{}
	err = json.Unmarshal(b, &m)
	if err != nil {
		err = errorx.Wrap(err, "json.Unmarshal")
		return
	}

	files := map[string]datax.M{}
	files[path] = m
	set.Load(files)
	return
}

func J加载配置文件2(path string) (tree *configx.ConfigRepo, err error) {
	b, err := ioutil.ReadFile(path)
	if err != nil {
		err = errorx.Wrap(err, "ioutil.ReadFile")
		return
	}
	m := map[string]interface{}{}
	err = json.Unmarshal(b, &m)
	if err != nil {
		err = errorx.Wrap(err, "json.Unmarshal")
		return
	}

	tree = configx.NewConfigRepo()
	files := map[string]datax.M{}
	files[path] = m
	tree.Load(files)
	return
}

func J加载数值文件(path string) (valueMap *values.ValuesMap, err error) {
	b, err := ioutil.ReadFile(path)
	if err != nil {
		err = errorx.Wrap(err, "ioutil.ReadFile")
		return
	}
	m := map[string]interface{}{}
	err = json.Unmarshal(b, &m)
	if err != nil {
		err = errorx.Wrap(err, "json.Unmarshal")
		return
	}

	valueMap = values.NewValuesMap()
	valueMap.ParseMap(m)
	return
}
